import Vue from 'vue'
import Router from 'vue-router' //导入路由器模块
import Home from '@/views/home/Home.vue'

//导入本地存储方法
import * as Auth from "@/utils/auth.js"
import {
	Toast,
	Dialog
} from "vant";

Vue.use(Router)

let router = new Router({

	//路由数组  （路由字典）
	routes: [{ //定义test路由
			path: '/test', //路由的路径
			name: 'test', //路由的名字
			component: () => import("@/views/test.vue"), //路由组件
			meta: {
				title: "Test",
			}
		},

		{ //定义重定向路由
			path: "/",
			redirect: "/home",
			meta: {
				title: "首页",
			}
		},
		{ //定义根路由
			path: '/home', //路由的路径
			name: 'home', //路由的名字
			component: Home, //路由组件
			meta: {
				title: "首页",
				alive: true, //是否缓存
			}
		},


		{ //定义商品列表路由
			path: '/product',
			name: 'product',
			component: () => import( /* webpackChunkName: "product" */ '@/views/product/Product.vue'),
			meta: {
				title: "商品",
				alive: true, //是否缓存
			}
		},

		{ //定义商品详情路由
			path: '/product/detail/:id',
			name: 'productDetail',
			component: () => import( /* webpackChunkName: "product" */ '@/views/product/ProductDetail.vue'),
			meta: {
				title: "商品详情"
			}
		},

		{ //定义购物车路由
			path: '/shopcart',
			name: 'shopcart',
			component: () => import( /* webpackChunkName: "product" */ '@/views/shopcart/Shopcart.vue'),
			meta: {
				title: "购物车",
				alive: true, //是否缓存
				requireAuth:true,  //这个路由需要认证
			}
		},

		{ //定义我的页面路由
			path: '/user',
			name: 'user',
			component: () => import( /* webpackChunkName: "user" */ '@/views/user/User.vue'),
			meta: {
				title: "我的",
				alive: true, //是否缓存
			}
		},

		{ //定义登录路由
			path: '/login',
			name: 'login',
			component: () => import( /* webpackChunkName: "user" */ '@/views/user/Login.vue'),
			meta: {
				title: "登录"
			}
		},

		{ //定义注册路由
			path: '/register',
			name: 'register',
			component: () => import( /* webpackChunkName: "user" */ '@/views/user/Register.vue'),
			meta: {
				title: "注册"
			}
		},

		{ //个人资料路由
			path: '/profile',
			name: 'profile',
			component: () => import( /* webpackChunkName: "user" */ '@/views/user/Profile.vue'),
			meta: {
				title: "个人资料"
			}
		},

		{ //修改密码路由
			path: "/user/updatePwd",
			component: () => import( /* webpackChunkName: "user" */ "@/views/user/UpdatePwd.vue"),
			name: "updatePwd",
			meta: {
				title: "修改密码"
			}
		},
		
		
		{ //定义订单创建路由
			path: '/order/create',
			name: 'orderCreate',
			component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderCreate.vue'),
			meta: {
				title: "新建订单"
			}
		},
		
		{ //定义订单列表路由
			path: '/order/list',
			name: 'orderList',
			component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderList.vue'),
			meta: {
				title: "订单列表"
			}
		},
		
		{ //定义订单明细路由
			path: '/order/detail/:id',//动态路由参数
			props:true,  //允许props接收
			name: 'orderDetail',
			component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderDetail.vue'),
			meta: {
				title: "订单详情"
			}
		},
		
		{ //定义地址列表路由
			path: '/address/list',
			name: 'addressList',
			component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressList.vue'),
			meta: {
				title: "地址列表"
			}
		},
		
		{ //定义地址编辑路由
			path: '/address/edit/:id', //动态路由参数
			props:true, //允许props接收
			name: 'addressEdit',
			component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressEdit.vue'),
			meta: {
				title: "地址编辑"
			}
		},
		
		
		{ //定义新闻列表路由
			path: '/news/list',
			name: 'newsList',
			component: () => import( /* webpackChunkName: "news" */ '@/views/news/NewsList.vue'),
			meta: {
				title: "新闻列表",			
			}
		},
		
		{ //定义新闻详情路由
			path: '/news/detail/:id', //动态路由参数
			props:true, //允许props接收
			name: 'newsDetail',
			component: () => import( /* webpackChunkName: "news" */ '@/views/news/NewsDetail.vue'),
			meta: {
				title: "新闻详情",			
			}
		},
		

		{
			path: "/working",
			component: () => import("@/views/Working.vue"),
			name: "working",
			meta: {
				title: "努力中...."
			},
		},

		{
			path: "*",
			name: "notFound",
			component: () => import( /* webpackChunkName: "notfound" */ '@/views/NotFound.vue'),
			meta: {
				title: "导航页面不存在"
			},
		},

	]
})


//设置全局路由导航守卫
router.beforeEach((to, from, next) => {
	
	if (to.name === 'login') { // 路由条状到login的，直接放行
		console.log(`${from.name}跳转到login`); // 控制台调试输出
		next(); // 路由继续往下跳转
		return;
	}

	if (!to.meta.requireAuth) { // 路由不需要验证的，直接放行
		console.log(`${from.name}跳转到${to.name},目的路由不需要用户认证`); // 控制台调试输出
		next();
		return;
	}

	// 路由需要验证的 
	const token = Auth.getTokenLocal(); //获取本地保存的Token
	if (!token) { // 如果token不存在的，则跳转登录login
		console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token不存在！强制跳转到login`); // 控制台调试输出
		Dialog.confirm({
			message: "没有Token,需要重新登录吗?",
		}).then(() => {
			// 需要登录，则跳转到登录login，并传递参数(登录后跳转的路由)
			next({
				name: 'login',
				params: {
					redirect: to.path
				}
			});
		}).catch(() => {
			// 不需要登录，跳转到首页 
			router.push({
				name: "home"
			});
		});

		return;
	}

	console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token也存在！`); // 控制台调试输出
	// 其余情况，则路由放行
	next(); //
});


export default router
